package io.gitee.chearnee.fw.datasecurity.core.executor.desensitization;

import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Set;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/10/2022 3:59 PM
 */
public class IterableSDExecutor implements SDExecutor<Iterable<?>> {

    @Override
    @SneakyThrows
    public Iterable<?> execute(Iterable<?> obj, Annotation generalField, Set<Class<?>> disabledClasses) {
        for (Object o : obj) {
            if (o instanceof String oString) {
                if (generalField == null) {
                    break;
                }
                if (disabledClasses.contains(generalField.annotationType())) {
                    break;
                }
                Class<?> aClass = o.getClass();
                Field stringValue = aClass.getDeclaredField("value");
                stringValue.trySetAccessible();
                String execute = SDExecutorFactory.getStringSdExecutor().execute(oString, generalField, disabledClasses);
                stringValue.set(o, execute.getBytes());
                continue;
            }
            SDExecutorFactory.getSDExecutor(o).executeObject(o, generalField, disabledClasses);
        }
        return obj;
    }
}
